package com.bxj.wxtt.spider;

import com.bxj.wxtt.api.domain.Adv;
import com.bxj.wxtt.api.domain.AdvContent;
import com.bxj.wxtt.api.domain.AdvRelation;
import com.bxj.wxtt.api.service.IAdvContentDubboService;
import com.bxj.wxtt.api.service.IAdvDubboService;
import com.bxj.wxtt.api.service.IAdvRelationDubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DataBasePipeline implements Pipeline {

    @Resource
    IAdvDubboService advDubboService;

    @Resource
    IAdvContentDubboService advContentDubboService;

    @Resource
    IAdvRelationDubboService advRelationDubboService;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void process(ResultItems resultItems, Task task) {
        //TODO 保存类目到数据库
        //TODO 保存商品到数据库
        List<Map<String, String>> list = resultItems.get("list");

        if (list == null)
            return;
        for (Map<String, String> map : list) {
            String type = map.get("mode");
            String id = map.get("id");

            if ("adv".equals(type)) {
                addAdv(map, id);
            } else if ("content".equals(type)) {
                String num = map.get("num");
                String content = map.get("content");
                String time = map.get("time");
                if ("1".equals(num)) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//                "time" -> "2018-07-18 05:27"
                    try {
                        Adv adv = new Adv();
                        adv.setDateTime(simpleDateFormat.parse(time));
                        adv.setArticleId(id);
                        advDubboService.updateAdvById(adv);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                AdvContent advContent = new AdvContent();
                advContent.setAdvId(id);
                advContent.setNum(Integer.parseInt(num));

                List<AdvContent> queryList = advContentDubboService.getAdvContentList(advContent);
                if (queryList.isEmpty()) {
                    advContent.setContent(content);
                    advContentDubboService.insert(advContent);
                } else {

                }
            } else if ("relation".contains(type)) {
                String recommandId = map.get("recommand_id");

                try {
                    AdvRelation relation = new AdvRelation();
                    relation.setAdvId(id);
                    relation.setRecommandId(recommandId);
                    List<AdvRelation> advRelationList = advRelationDubboService.getAdvRelationList(relation);
                    if (advRelationList.isEmpty())
                        advRelationDubboService.insert(relation);
                } catch (Exception e) {
                }
                addAdv(map, id);
            } else {
                logger.info("unknown type {}", type);
            }
        }
    }

    private void addAdv(Map<String, String> map, String id) {
        String title = map.get("title");
        boolean inserted = false;
        Adv adv = advDubboService.selectById(id);
        if (adv == null) {
            inserted = true;
            adv = new Adv();
        }
        adv.setArticleId(id);
        adv.setFlag(1);
        adv.setChannelId(Integer.parseInt(map.get("channelId")));
        adv.setDateTime(new Date());
        String href = map.get("href");
        adv.setHref(href);
        adv.setImg1(map.get("img"));
        adv.setImg2(map.get("img1"));
        adv.setTitle(title);
        adv.setType(Integer.parseInt(map.get("type")));
        adv.setOrigin(map.get("origin"));
        if (inserted)
            advDubboService.insert(adv);
        else
            advDubboService.updateAdvById(adv);
    }
}